package com.lzq.model.command;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author : modify
 * @version : v1.0
 * @description :
 * @createTime : 2022/11/28 10:13
 * @updateUser : modify
 * @updateTime : 2022/11/28 10:13
 * @updateRemark : 命令模式
 */
public class Order {
    private int no;
    private Map<String, Integer> foodDic = new HashMap<String, Integer>();

    public int getNo() {
        return no;
    }

    public Map<String, Integer> getFoodDic() {
        return foodDic;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setFoodDic(String name, Integer num) {
        this.foodDic.put(name, num);
    }
}

class SeniorChef {
    public void makeFood(int num, String foodName) {
        System.out.println(num + "份" + foodName);
    }
}

interface Command {
    /**
     * 的执行方法
     */
    void execute();
}

class OrderCommand implements Command {
    private SeniorChef receiver;
    private Order order;

    public OrderCommand(SeniorChef receiver, Order order) {
        this.receiver = receiver;
        this.order = order;
    }

    @Override
    public void execute() {
        System.out.println(order.getNo() + " 订单开始");
        Set<String> keys = order.getFoodDic().keySet();
        for (String key : keys) {
            receiver.makeFood(order.getFoodDic().get(key), key);
        }
        System.out.println(order.getNo() + " 订单结束");
    }
}

class Waitor {
    private ArrayList<Command> commands;

    public Waitor() {
        this.commands = new ArrayList<>();
    }

    public void setCommand(Command cmd) {
        commands.add(cmd);
    }

    public void orderUp() {
        System.out.println("新订单来了.......");
        for (int i = 0; i < commands.size(); i++) {
            Command cmd = commands.get(i);
            if (cmd != null) {
                cmd.execute();
            }
        }
    }
}

class Test {
    public static void main(String[] args) {
        Order order = new Order();
        order.setNo(1);
        order.setFoodDic("辣椒炒肉", 1);
        order.setFoodDic("紫菜蛋花汤", 1);

        Order order2 = new Order();
        order2.setNo(2);
        order2.setFoodDic("烤鱿鱼", 3);
        order2.setFoodDic("可乐", 1);


        SeniorChef seniorChef = new SeniorChef();
        OrderCommand orderCommand = new OrderCommand(seniorChef, order);
        OrderCommand orderCommand2 = new OrderCommand(seniorChef, order2);

        Waitor waitor = new Waitor();
        waitor.setCommand(orderCommand);
        waitor.setCommand(orderCommand2);

        waitor.orderUp();
    }
}